using System;
using System.Collections.Generic;
using System.Windows.Forms;
using smartBrowser.GUI;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
using smartBrowser.Util;
using XPExplorerBar;
using smartBrowser.Task.Annotators;

namespace smartBrowser.Task
{
    class JavaScriptTask : Task
    {

        public JavaScriptTask(WebSite webSite, TabPageForm form)
            : base(webSite, form)
        {
        }

        protected override void Execute()
        {
            HtmlDocument document = webSite.SiteDocument;

            document.Body.AppendChild(CreateTooltipScripts(document));

            FireCompleteEvent();
        }

        private HtmlElement CreateTooltipScripts(HtmlDocument document)
        {
            string script = @"
                hideTooltipTimer = 0;
                activeTooltip = null;

                function findPos(obj) {
			        var curleft = curtop = 0;
			        if (obj.offsetParent) {
				        curleft = obj.offsetLeft
				        curtop = obj.offsetTop
				        while (obj = obj.offsetParent) {
					        curleft += obj.offsetLeft
					        curtop += obj.offsetTop
				        }
			        }
			        return [curleft, curtop];
		        }

		        function showTooltip(event) {
			        if (!event)
				        var event = window.event;

			        var element = event.currentTarget || event.toElement;
                    var tooltip = null;

                    while ((tooltip = document.getElementById(element.getAttribute('tooltip'))) == null) {
                        element = element.parentNode;
                    }

			        positionTooltip(element, tooltip, event);
    
                    if (activeTooltip != tooltip) {
                        if (activeTooltip != null)
                            activeTooltip.style.visibility='hidden';

			            tooltip.style.visibility='visible';
                        activeTooltip = tooltip;
                    }
                    clearHideTimer();
		        }

		        function tooltipMouseOut(event) {
			        if (!event)
				        var event = window.event;

			        var fromElement = event.currentTarget || event.fromElement;
			        var toElement = event.relatedTarget || event.toElement;

			        hideTooltipTimer = setTimeout(function() { hideTooltip(fromElement, toElement); }, 300);
		        }

		        function hideTooltip(fromElement, toElement) {
                    if (activeTooltip != null)
                        activeTooltip.style.visibility='hidden';
                    activeTooltip = null;
		        }

		        function positionTooltip(element, tooltip, event) {
                    offset = findPos(element);

			        posX = offset[0] + element.offsetWidth / 2 - tooltip.offsetWidth / 2;
			        posY = offset[1] - tooltip.offsetHeight - 4;

			        if (posX < 0)
				        posX = 0;

                    if (posX + tooltip.offsetWidth > document.body.clientWidth)
				        posX = document.body.clientWidth - tooltip.offsetWidth;

			        if (posY < 0)
				        posY = offset[1] + element.offsetHeight;

			        tooltip.style.top = posY;
			        tooltip.style.left = posX;
		        }

		        function tooltipMouseOver(event) {
			        clearHideTimer();
		        }

		        function clearHideTimer()
		        {
			        clearTimeout(hideTooltipTimer);
		        }
                ";

            HtmlElement scriptElm = document.CreateElement("script");
            scriptElm.SetAttribute("language", "javascript");
            mshtml.HTMLScriptElement scriptElm2 = (mshtml.HTMLScriptElement)scriptElm.DomElement;
            scriptElm2.text = script;

            return scriptElm;
        }
    }
}
